var express = require('express');
var router = express.Router();
var userDao = require('../dao/userDao');
var fs = require('fs');
/* GET users listing. */
router.get('/', function(req, res, next) {
	console.log('----------------------------')
  	res.send('respond with a resource');
});


var dirname = __dirname.replace('routes','');
// 登录
router.get('/login', function(req, res, next) {
	console.log(req.body);
	userDao.login(req,res,next);
});
router.post('/login', function(req, res, next) {

	console.log(req.body);
	console.log('login')
	console.log('-----------------------------')
	userDao.login(req,res,next);

});

router.get('/loginout',function(req,res,next){
	req.session.destroy(function(err){
		res.send(err);
	});
});

// 用户名是否存在
router.get('/queryByName', function(req, res, next) {
	console.log(req.body);
	
	userDao.queryByName(req,res,next);
});
router.post('/queryByName', function(req, res, next) {

	console.log(req.body);
	userDao.queryByName(req,res,next);
});

//用户注册
router.get('/insert', function(req, res, next) {
	console.log(req.body);
	
	userDao.insert(req,res,next);
});
router.post('/insert', function(req, res, next) {

	console.log(req.body);
	userDao.insert(req,res,next);
});

//logo
router.get('/logo', function(req, res, next) {
	console.log(req.body);
	
	//userDao.insert(req,res,next);
});
router.post('/logo', function(req, res, next) {

	console.log(req.body);

	//userDao.insert(req,res,next);
	
	var image = req.body.file;
	var name = req.body.name?req.body.name:'1.png';
	if(image){
		var data = image.replace(/^data:image\/\w+;base64,/, '');
		var path = 'upload/logo/'+name;
		fs.writeFile(path, data,'base64',function(err){
			if(err){
				res.send('1');
				throw err;
			}
			res.send('11');
		});
	}else{
		res.send('0');
	}

	

	
});





module.exports = router;
